<template>
  <div class="comment-post">
    <van-field
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
      class="post-filed"
    />
    <van-button type="default" :disabled="message === ''" @click="postComment"
      >发布
    </van-button>
  </div>
</template>

<script>
import { postCommentsAPI } from '@/api'
export default {
  name: '',
  components: {},
  props: {
    target: {
      type: String,
      required: true
    },
    art_id: String
  },
  data() {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    // 发布评论
    async postComment() {
      try {
        const { data } = await postCommentsAPI({
          target: this.target,
          content: this.message,
          art_id: this.art_id || null
        })
        this.$emit('commentPost', data.data.new_obj)
        this.$toast.success('发布评论成功')
      } catch (error) {
        if (error.response.status === 401) {
          return this.$toast.fail('请先登录')
        }
        if (error.response.status === 403) {
          return this.$toast.fail('本文暂不允许评论')
        }
        this.$toast.fail('评论失败')
      } finally {
        this.message = ''
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 0.42667rem 0 0.42667rem 0.42667rem;
  .post-filed {
    background-color: #f5f7f9;
    color: #646566;
  }
  .van-button {
    width: 2rem;
    border: none;
    padding: 0;
    color: #6ba3d8;
  }
}
</style>
